package cn.weekimwee.firstlineofcodeforkotlin.unit6

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import org.jetbrains.anko.toast

/**
 * Created by Wee Kim Wee on 2018/2/18.
 */
class MyDatabaseHelper(val context:Context,name:String,factory:SQLiteDatabase.CursorFactory?,version:Int): SQLiteOpenHelper(context,name, factory, version) {

    val CREATE_BOOK= "create table Book (" +
            "id integer primary key autoincrement," +
            "author text," +
            "price real," +
            "pages integer," +
            "name text)"

    val CREATE_CATEGORY = "create table Category (" +
            "id integer primary key autoincrement," +
            "category_name text" +
            "category_code integer)"

    override fun onCreate(db: SQLiteDatabase?) {
        db?.execSQL(CREATE_BOOK)
        db?.execSQL(CREATE_CATEGORY)
//        context.toast("create succeeded")
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        db?.execSQL("drop table if exists Book")
        db?.execSQL("drop table if exists Category")
        onCreate(db)
    }

}